//
//  ServiceConfig.m
//  ShopInMall_OC
//
//  Created by Sailer Guo on 2019/10/11.
//  Copyright © 2019 Sailer Guo. All rights reserved.
//

#import "CenterServiceConfig.h"

@interface CenterServiceConfig ()

@end

@implementation CenterServiceConfig

+ (CenterServiceConfig *)serviceConfigWith:(EnvironmentType)env {
    switch (env) {
        case Local:
            return [self localServiceConfig];
            break;
        
        case Development:
            return [self developmentServiceCofig];
            
        case Production:
            return [self productionServiceConfig];
            break;
        default:
            NSAssert(NO, @"Unknown env");
            return nil;
            break;
    }
}

/*
 { "code": "A00000", "data": true, "errorMessage": "成功" }
 center 对应中间件的接口，所有的返回格式如下
 */
- (BOOL)successWitResponseObject:(id)responseObject {
    return [responseObject[@"code"] isEqualToString:@"A00000"];
}

- (id)responseData:(id)responseObject withUrl:(nonnull NSString *)url isList:(BOOL)isList {
    
    // 退货订单没有返回标准的数据格式，直接透传
    if ([url isEqualToString:@"refund/selectRefundApplyByParamToWeChat"]) {
        return responseObject;
    }
    
    if (isList) {
        return responseObject[@"data"][@"list"];
    } else {
        return responseObject[@"data"];
    }
}

/*
{ "code": "A00000", "data": true, "errorMessage": "成功" }
center 对应中间件的接口，所有的返回格式如下
*/
- (NSError *)errorMessage:(id)responseObject withUrl:(nonnull NSString *)url {
    /*
     B03BD001 因为后端返回的状态码不是int类型，而且格式不标准，无法统一处理。
     所以用一个状态码 10004 统一表示没有返回正确数据的情况，这个定义在ServiceConfigProtocol 这个文件中
     */
    NSDictionary *userInfo;
    if (responseObject[@"errorMessage"]) {
        userInfo = @{@"errorMessage":responseObject[@"errorMessage"]};
    }
    NSError *error = [NSError errorWithDomain:NSErrorDomainNoAdaptiveData code:NSSPErrorCode userInfo:userInfo];
    return error;
}

#pragma mark - Private
+ (CenterServiceConfig *)productionServiceConfig {
    CenterServiceConfig *serviceConfig = [[CenterServiceConfig alloc] init];
    serviceConfig.baseUrl = @"https://app.shopin.cn/center/";
    serviceConfig.baseImageUrl = @"http://images.shopin.net/images";
    [serviceConfig configSession];
    return serviceConfig;
}

+(CenterServiceConfig *)localServiceConfig {
    CenterServiceConfig *serviceConfig = [[CenterServiceConfig alloc] init];
    serviceConfig.baseUrl = @"http://192.168.110.178:10480/";
    serviceConfig.baseImageUrl = @"http://images.shopin.net/images";
    [serviceConfig configSession];
    return serviceConfig;
}

+ (CenterServiceConfig *)developmentServiceCofig {
    CenterServiceConfig *serviceConfig = [[CenterServiceConfig alloc] init];
    serviceConfig.baseUrl = @"http://192.168.110.178:10480/";
    serviceConfig.baseImageUrl = @"http://images.shopin.net/images";
    [serviceConfig configSession];
    return serviceConfig;
}

- (void)configSession {
    AFHTTPSessionManager *httpRequestManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:self.baseUrl] sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableSet *contentTypes = [NSMutableSet setWithSet:httpRequestManager.responseSerializer.acceptableContentTypes];
    
    [contentTypes addObject:@"content-type"];
    [contentTypes addObject:@"application/json;charse=UTF-8"];
    
    httpRequestManager.responseSerializer.acceptableContentTypes = contentTypes;
    
    httpRequestManager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    httpRequestManager.securityPolicy.allowInvalidCertificates = YES;
    
    AFSecurityPolicy *securityPoliy = [AFSecurityPolicy defaultPolicy];
    //allowInvalidCertificates 是否允许无效证书（也就是自建的证书），默认为NO
    //如果是需要验证自建证书，需要设置为YES
    securityPoliy.allowInvalidCertificates = YES;
    securityPoliy.validatesDomainName = NO;
    httpRequestManager.securityPolicy = securityPoliy;
    
    session = httpRequestManager;
}


@synthesize baseUrl;

@synthesize baseImageUrl;

@synthesize env;

@synthesize session;

@end
